package cn.yunhe.service;

import cn.yunhe.dao.INewsDao;
import cn.yunhe.pojo.NewsDemo;
import cn.yunhe.pojo.PageDemo;
import cn.yunhe.sqlsessionutil.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class NewsService {
    SqlSession session = SqlSessionUtil.getSqlSession();
    INewsDao newsDao =  session.getMapper(INewsDao.class);
    //添加新闻
    public int addNews(NewsDemo newsdemo){
      int flag =  newsDao.addNewsByNews(newsdemo);
      session.commit();
      return  flag;
    }
    //获取新闻的总条数
    public int getcounts(PageDemo  pagedemo){
        return   newsDao.searchAllCounts(pagedemo);
    }
     // 分页查询 获取每页的新闻
    public List<NewsDemo> getNewsByLimit(PageDemo pageDemo){
        return newsDao.serachAllNews(pageDemo);
    }
   //根据新闻id 查询所有新闻内容
    public NewsDemo searchNewsById(int newid){
        return  newsDao.searchNewsById(newid);
    }
    //根据新闻实体类中的新闻id 修改新闻
    public int updateNewsByNewsId(NewsDemo newsdemo){
      int flag =  newsDao.upadteNewsByNews(newsdemo);
      session.commit();
      return  flag;
    }

    public int deleteNewsByNewId(int newId){
        int flag = newsDao.delNewsById(newId);
        session.commit();
        return  flag;
    }

    public List<NewsDemo> serachNewsByWord(String keyword){
        return newsDao.searchLikeByWord(keyword);
    }
}
